package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.DeleteException;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.exception.sql.UpdateException;
import com.fantastic.common.utils.FilesUtils;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.mapper.zjs.CartMapper;
import com.fantastic.model.pojo.zjs.Cart;
import com.fantastic.model.vo.zjs.CartVo;
import com.fantastic.service.zjs.CartService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/6/5 19:16
 * @description: 购物车信息service接口实现类
 */
@Service
public class CartServiceImpl implements CartService {

    @Resource
    private CartMapper cartMapper;


    /**
     * 查询购物车信息
     *
     * @param cart
     * @return
     */
    @Override
    public List<Cart> listCart(Cart cart) {
        List<Cart> carts = cartMapper.listCart(cart);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), carts);
        return carts;
    }

    /**
     * 查询单个账号或是单条购物车信息
     *
     * @param cart
     * @return
     */
    @Override
    public Cart getCart(Cart cart) {
        if (cart.getAccountId() == null && cart.getVarietyId() == null && cart.getMealId() == null) {
            throw new SelectException(RespStatusInfo.DATA_ERROR);
        }
        return cartMapper.getCart(cart);
    }

    /**
     * 查询单个账号的所有购物车信息
     *
     * @param accountId 账号id
     * @return
     */
    @Override
    public List<CartVo> listCartVo(Integer accountId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.DATA_ERROR), accountId);
        List<CartVo> cartVos = cartMapper.listCartVo(accountId);
        for (CartVo cartvo : cartVos) {
            if (cartvo.getVariety() != null) {
                cartvo.getVariety().setVarietyPicture(FilesUtils.getSlashParse(cartvo.getVariety().getVarietyPicture()));
            }
            if (cartvo.getMeal() != null) {
                cartvo.getMeal().setMealPicture(FilesUtils.getSlashParse(cartvo.getMeal().getMealPicture()));
            }
        }
        return cartVos;
    }


    /**
     * 添加购物车信息
     *
     * @param cart
     * @return
     */
    @Override
    public Integer insertCart(Cart cart) {
        Integer num = null;
        //账号id并且菜品id或是套餐id不为null
        if (cart.getAccountId() == null && (cart.getVarietyId() == null || cart.getMealId() == null)) {
            throw new InsertException(RespStatusInfo.DATA_ERROR);
        } else {
            //查询这条购物车信息否存在。
            Cart getCart = getCart(cart);
            if (getCart != null) {
                //判断账号不为空并且套餐id不为空 （表示是重复数据进行修改）
                if (getCart.getAccountId() != null && getCart.getVarietyId() != null) {
                    cart.setCartPrice(getCart.getCartPrice().add(cart.getCartPrice()));
                    cart.setCartCount(getCart.getCartCount() + cart.getCartCount());
                    num = cartMapper.updateCart(cart);
                }
                //判断账号不为空并且套餐id不为空 （表示是重复数据进行修改）
                else if (getCart.getAccountId() != null && getCart.getMealId() != null) {
                    cart.setCartPrice(getCart.getCartPrice().add(cart.getCartPrice()));
                    cart.setCartCount(getCart.getCartCount() + cart.getCartCount());
                    num = cartMapper.updateCart(cart);
                } else {
                    //添加新数据
                    num = cartMapper.insertCart(cart);
                }
            } else {
                //添加新数据
                num = cartMapper.insertCart(cart);
            }

        }
        return num;
    }

    /**
     * 修改购物车信息
     *
     * @param cart
     * @return
     */
    @Override
    public Integer updateCart(Cart cart) {
        //三个都null抛异常
        if (cart.getAccountId() != null && (cart.getVarietyId() != null || cart.getMealId() != null)) {
            //判断cart的对象里的价格和数量是否为0,不为0表示不删除，单纯进行修改
            if (cart.getCartPrice() != null && cart.getCartCount() != null) {
                //执行修改
                return cartMapper.updateCart(cart);
            } else {
                //进行删除数据
                return cartMapper.deleteCart(cart);
            }
        } else {
            throw new UpdateException(RespStatusInfo.DATA_ERROR);
        }
    }

    /**
     * 删除购物车信息
     *
     * @param cart
     * @return
     */
    @Override
    public Integer deleteCart(Cart cart) {
        if (cart.getAccountId() != null || (cart.getVarietyId() != null || cart.getMealId() != null)) {
            return cartMapper.deleteCart(cart);
        } else {
            throw new DeleteException(RespStatusInfo.DATA_ERROR);
        }
    }

}
